package cn.wgx.module.ent.web;

import cn.wgx.common.base.IBaseController;
import cn.wgx.common.entity.ent.EntUserApp;
import cn.wgx.common.entity.ent.Enterprise;
import cn.wgx.common.entity.sys.SysApp;
import cn.wgx.common.entity.sys.SysUser;
import cn.wgx.common.security.client.config.entity.LoginAppUser;
import cn.wgx.common.security.util.SysUserUtil;
import cn.wgx.common.util.ResponseDataUtil;
import cn.wgx.module.ent.service.EntUserAppService;
import cn.wgx.module.ent.service.EnterpriseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "5.2.1 企业用户-应用管理")
@RestController
@RequestMapping("/api/entUserApp")
public class EntUserAppCtrl extends IBaseController<EntUserAppService, EntUserApp> {

    @Autowired
    EnterpriseService enterpriseService;

    @ApiOperation(value = "根据实体参数查询列表,分页查询(page第几页, limit每页数量,默认20)")
    @GetMapping("list")
    public Map list(@ModelAttribute EntUserApp entUserApp,
                    @RequestParam(required = false, defaultValue = "1") Integer page,
                    @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(page1, service.getEntUserApps(page1, new QueryWrapper<>(entUserApp)));
    }

    @ApiOperation(value = "查询未授权用户的应用,分页查询(page第几页, limit每页数量,默认20)")
    @GetMapping("listAppNotInEntUser")
    public Map listAppNotInEnt(@RequestParam Integer ent_user_id,
                               @RequestParam Integer ent_id,
                               @RequestParam(required = false, defaultValue = "1") Integer page,
                               @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page<SysApp> page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(page1, service.getAppNotInEntUser(page1, ent_user_id, ent_id));
    }

    @ApiOperation(value = "查询未开通应用的用户,分页查询(page第几页, limit每页数量,默认20)")
    @GetMapping("listUserNotInEntApp")
    public Map listUserNotInEntApp(@RequestParam Integer app_id,
                                   @RequestParam Integer ent_id,
                                   @RequestParam(required = false) String nickname,
                                   @RequestParam(required = false, defaultValue = "1") Integer page,
                                   @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page<SysUser> page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(page1, service.getUserNotInEntApp(page1, app_id, ent_id, nickname));
    }
//
//    @ApiOperation(value = "新增实体数据", notes = "必填：ent_user_id,app_id,ent_id")
//    @PostMapping("add")
//    public Map add(@RequestBody EntUserApp t) {
//        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
//        if(t.getEnt_id() == null){
//            if(t.getEntCode() == null){
//                ResponseDataUtil.returnErrorMap("缺少entId或entCode");
//            }
//            Enterprise byCode = enterpriseService.getByCode(t.getEntCode());
//            if(byCode != null){
//                t.setEnt_id(byCode.getEnt_id());
//            }else{
//                ResponseDataUtil.returnErrorMap("entCode error");
//            }
//        }
//        Enterprise byId = enterpriseService.getById(t.getEnt_id());
//        if (byId == null || t.getEnt_id() == null || t.getEnt_user_id() == null || t.getApp_id() == null) {
//            return ResponseDataUtil.returnErrorMap("缺少参数");
//        }
//        if (!loginAppUser.getUser_id().equals(byId.getCreateUserId())) {
//            return ResponseDataUtil.returnErrorMap("非企业管理员不可以操作");
//        }
//        service.save(t);
//        return ResponseDataUtil.returnData(t);
//    }

    @ApiOperation(value = "新增实体数据-批量", notes = "必填：ent_user_id,app_id,ent_id")
    @PostMapping("addBatch")
    public Map addBatch(@RequestBody List<EntUserApp> t) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        for (EntUserApp entUserApp : t) {
            if(entUserApp.getEnt_id() == null){
                if(entUserApp.getEntCode() == null){
                    ResponseDataUtil.returnErrorMap("缺少entId或entCode");
                }
                Enterprise byCode = enterpriseService.getByCode(entUserApp.getEntCode());
                if(byCode != null){
                    entUserApp.setEnt_id(byCode.getEnt_id());
                }else{
                    ResponseDataUtil.returnErrorMap("entCode error");
                }
            }
            Enterprise byId = enterpriseService.getById(entUserApp.getEnt_id());
            if (byId != null && !loginAppUser.getUser_id().equals(byId.getCreateUserId())) {
                return ResponseDataUtil.returnErrorMap("非企业管理员不可以操作");
            }
        }
        service.saveBatch(t);
        return ResponseDataUtil.returnData(t);
    }

    @ApiOperation(value = "根据主键ID删除数据", notes = "必填：eua_id")
    @DeleteMapping("del/{id}")
    public Map del(@PathVariable Integer id) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        EntUserApp byId = service.getById(id);
        if (byId != null && (!loginAppUser.getUser_id().equals(byId.getCreateUserId()))) {
            return ResponseDataUtil.returnErrorMap("非企业管理员不可以操作");
        }
        return ResponseDataUtil.returnData(service.removeById(id));
    }

    @ApiOperation(value = "根据主键ID删除数据-批量", notes = "必填：eua_id")
    @DeleteMapping("delBatch")
    public Map delBatch(@RequestBody List<Integer> ids) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        for (Integer id : ids) {
            EntUserApp byId = service.getById(id);
            if (byId != null && (!loginAppUser.getUser_id().equals(byId.getCreateUserId()))) {
                return ResponseDataUtil.returnErrorMap("非企业管理员不可以操作");
            }
        }
        return ResponseDataUtil.returnData(service.removeByIds(ids));
    }
}
